package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.utils.PathMatcherUtil;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.utils.PathMatcherUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 完善登录功能
 * 第一步：自定义过滤器类 实现未登录不能访问后台页面
 */
@Slf4j
@WebFilter //固定：注册spring容器
public class LoginCheckFilter implements Filter {

    /**
     * 完善登录功能
     * 第三步：实现具体过滤处理
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long id = Thread.currentThread().getId();
        log.debug("*****************LoginCheckFilter************线程id为{}**",id);
        HttpServletRequest httpServletRequest =  (HttpServletRequest)request;
        HttpServletResponse httpServletResponse =  (HttpServletResponse)response;
        //1、获取本次请求的URI 例如：/backend/index.html
        String requestURI = httpServletRequest.getRequestURI();
        //2、判断本次请求是否需要处理
        //哪些请求放行？登录相关资源进行放行
        //urls：需要放行的资源
        //requestURI：目前请求的URI
        String[] urls = new String[]{
                "/**/login.html","/**/images/**","/**/js/**","/**/plugins/**","/**/styles/**","favicon.ico"
                ,"/**/api/**","/**/fonts/**","/employee/login","/employee/logout","/user/sendMsg","/user/login"
                ,"/front/page/login.html","/backend/page/login/**","/doc.html","/webjars/**","/swagger-resources"
                ,"/v2/api-docs"
        };
        if(PathMatcherUtil.check(urls,requestURI)){
            //3、如果不需要处理，则直接放行
            chain.doFilter(request,response);
            return;//一定要加上return
        }
        //4、判断登录状态，如果已登录，则直接放行
        HttpSession session = httpServletRequest.getSession();
        Employee employee = (Employee)session.getAttribute("employee");
        if(employee != null){
            //用户信息不为空 设置当前用户id
            BaseContext.setCurrentId(employee.getId());
            chain.doFilter(request,response);
            return;//一定要加上return
        }
        //5、如果未登录则返回未登录结果
//        httpServletResponse.sendRedirect("/backend/page/login/login.html");//重定向登录页面

        //移动端页面过滤
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        log.debug("**********user**********"+user);
        if(user != null){
            log.info("移动用户已登录，用户id为：{}",httpServletRequest.getSession().getAttribute("user"));
            BaseContext.setCurrentId(user.getId());
            chain.doFilter(httpServletRequest,httpServletResponse);
            return;
        }

        log.debug("************则跳转首页**********");

        if(requestURI.contains("backend")){
            httpServletResponse.sendRedirect("/backend/page/login/login.html");
        }else {
            httpServletResponse.sendRedirect("/front/page/login.html");
        }
    }
}
